package com.etang.a2020b1006;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;

import androidx.appcompat.app.AppCompatActivity;

import com.bizideal.smarthome.socket.SocketClient;
import com.etang.a2020b1006.fragment.BarActivity;
import com.etang.a2020b1006.tools.CodePass;
import com.etang.a2020b1006.tools.DiyToast;
import com.etang.a2020b1006.tools.MyDataBaseHelper;

public class MainActivity extends AppCompatActivity {

    private Button btn_login;
    private EditText et_user, et_pass, et_yanzheng;
    private String user, pass, yanzheng;
    private CheckBox cb_rember, cb_autologin;
    private MyDataBaseHelper dbHelper;
    private SQLiteDatabase db;
    private ImageView iv_code, iv_setting;
    private SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        cehck_first();
        check_rember();
        iv_code.setImageBitmap(CodePass.createBitmap(getApplicationContext()));
        et_yanzheng.setText(CodePass.code);
        iv_code.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iv_code.setImageBitmap(CodePass.createBitmap(getApplicationContext()));
                et_yanzheng.setText(CodePass.code);
            }
        });
        iv_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show_popwindow();
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                user = et_user.getText().toString();
                pass = et_pass.getText().toString();
                yanzheng = et_yanzheng.getText().toString();
                if (user.isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "用户名不能为空");
                } else if (pass.isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "密码不能为空");
                } else if (yanzheng.isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "验证码不能为空");
                } else {
                    Cursor cursor = db.rawQuery("select * from user where username = ? and passward = ?", new String[]{et_user.getText().toString(), et_pass.getText().toString()});
                    if (cursor.moveToNext()) {
                        if (yanzheng.equalsIgnoreCase(CodePass.code)) {
                            if (cb_autologin.isChecked()) {
                                sharedPreferences.edit()
                                        .putBoolean("autologin", true)
                                        .putBoolean("rember", true)
                                        .putString("user", user)
                                        .putString("pass", pass).commit();
                            } else if (cb_rember.isChecked()) {
                                sharedPreferences.edit()
                                        .putBoolean("autologin", false)
                                        .putBoolean("rember", true)
                                        .putString("user", user)
                                        .putString("pass", pass).commit();
                            } else {
                                sharedPreferences.edit()
                                        .putBoolean("autologin", false)
                                        .putBoolean("rember", false)
                                        .putString("user", user)
                                        .putString("pass", pass).commit();
                            }
                            SocketClient.ip = sharedPreferences.getString("ip", null);
                            startActivity(new Intent(getApplicationContext(), LoadingActivity.class));
                            finish();
                        } else {
                            DiyToast.showToast(getApplicationContext(), "验证码输入错误");
                        }
                    } else {
                        DiyToast.showToast(getApplicationContext(), "用户名或密码输入错误");
                    }
                }
            }
        });
    }

    private void show_popwindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_pop_ip, null, false);
        final PopupWindow popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
        popupWindow.showAsDropDown(iv_setting);
        final EditText et_ip = (EditText) view.findViewById(R.id.et_pop_ip);
        final Button btn_con = (Button) view.findViewById(R.id.btn_pop_con);
        btn_con.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (et_ip.getText().toString().isEmpty()) {
                    DiyToast.showToast(getApplicationContext(), "请输入IP地址");
                } else {
                    sharedPreferences.edit().putString("ip", et_ip.getText().toString());
                    popupWindow.dismiss();
                }
            }
        });
    }

    private void cehck_first() {
        if (sharedPreferences.getBoolean("qwe", false) == true) {
        } else {
            sharedPreferences.edit().putBoolean("qwe", true).commit();
            sharedPreferences.edit().putString("ip", "18.1.10.2").commit();
            sharedPreferences.edit().putString("smo_state", "开").commit();
            sharedPreferences.edit().putString("ill_state", "开").commit();
            sharedPreferences.edit().putString("per_state", "开").commit();
            sharedPreferences.edit().putString("temp_state", "开").commit();
            sharedPreferences.edit().putString("pm_state", "开").commit();

            sharedPreferences.edit().putString("smo_values", "100").commit();
            sharedPreferences.edit().putString("ill_values_low", "200").commit();
            sharedPreferences.edit().putString("ill_values_max", "300").commit();
            sharedPreferences.edit().putString("per_values", "5").commit();
            sharedPreferences.edit().putString("temp_values_low", "18").commit();
            sharedPreferences.edit().putString("temp_values_max", "26").commit();
            sharedPreferences.edit().putString("pm_values_low", "75").commit();
            sharedPreferences.edit().putString("pm_values_max", "75").commit();
        }
    }

    private void check_rember() {
        if (sharedPreferences.getBoolean("rember", false) == true) {
            cb_rember.setChecked(true);
            et_user.setText(sharedPreferences.getString("user", null));
            et_pass.setText(sharedPreferences.getString("pass", null));
        }
        if (sharedPreferences.getBoolean("autologin", false) == true) {
            SocketClient.ip = sharedPreferences.getString("ip", null);
            startActivity(new Intent(getApplicationContext(), BarActivity.class));
            finish();
        }
    }

    private void initView() {
        sharedPreferences = getSharedPreferences("rember", MODE_PRIVATE);
        iv_code = (ImageView) findViewById(R.id.iv_code);
        btn_login = (Button) findViewById(R.id.btn_login);
        et_pass = (EditText) findViewById(R.id.et_pass);
        et_user = (EditText) findViewById(R.id.et_user);
        iv_setting = (ImageView) findViewById(R.id.iv_setting);
        et_yanzheng = (EditText) findViewById(R.id.et_yanzheng);
        cb_autologin = (CheckBox) findViewById(R.id.cb_autologin);
        cb_rember = (CheckBox) findViewById(R.id.cb_rember);
        dbHelper = new MyDataBaseHelper(getApplicationContext(), "info.db", null, 2);
        db = dbHelper.getWritableDatabase();
    }
}